package club.caihappy.jedis;

import com.sun.xml.internal.bind.v2.model.core.ID;
import redis.clients.jedis.Jedis;

import java.util.Random;

/**
 * Created by DELL on 2021/12/5
 **/
public class PhoneCode {


    public static void main(String[] args) {
        verifyCode("15953285601");
        getRedisCode("15953285601","313225");
    }

    public static void getRedisCode(String phone, String code) {
        Jedis jedis = new Jedis("101.43.5.132", 6379);
        String codeKey = "Verify" + phone + ":code";
        String rCode = jedis.get(codeKey);
        if (rCode.equals(code)) {
            System.out.println("success");
        } else {
            System.out.println("fail");
        }
        jedis.close();
    }

    public static void verifyCode(String phone) {
        Jedis jedis = new Jedis("101.43.5.132", 6379);
        String countKey = "Verify" + phone + ":count";
        String codeKey = "Verify" + phone + ":code";

        String count = jedis.get(countKey);
        if (count==null) {
            jedis.setex(countKey, 24 * 60 * 60, "1");
        } else if (Integer.parseInt(count) < 3) {
            jedis.incr(countKey);
        } else if (Integer.parseInt(count) >= 3) {
            System.out.println("发送三次,不能再发送");
            jedis.close();
            return;
        }

        String vcode = getCode();
        jedis.setex(codeKey, 120, vcode);
        jedis.close();
    }


    public static String getCode() {

        String code = "";
        for (int i = 0; i < 6; i++) {
            int a = new Random().nextInt(10);
            if (i == 0 & a == 0) {
                a++;
            }
            code += a;
        }

        return code;

    }
}
